home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / codelib7 / v_09_06 / phillips.exe / RTIFF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-02  |  5.8 KB  |  213 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.        /**************************************************
  8.        *
  9.        *       file d:\cips\rtiff.c
  10.        *
  11.        *       Functions: This file contains
  12.        *          read_tiff_image
  13.        *          read_line
  14.        *          seek_to_first_line
  15.        *          seek_to_end_of_line
  16.        *
  17.        *       Purpose:
  18.        *  These functions read a TIFF image and insert
  19.        *  the data into a ROWSxCOLS array of short.
  20.        *
  21.        *
  22.        *       External Calls:
  23.        *  mof.c - my_open
  24.        *  mrw.c - my_read
  25.        *  tiff.c - read_tiff_header
  26.        *
  27.        *       Modifications:
  28.        *       25 June 1990 - created
  29.        *
  30.        ******************************************************/
  31.  
  32.  
  33. #include "d:\cips\cips.h"
  34.  
  35.  
  36.  
  37.  
  38.  
  39. read_tiff_image(image_file_name, array, il, ie, ll, le)
  40.  
  41.       char   image_file_name[];
  42.       int    il, ie, ll, le;
  43.         short   array[ROWS][COLS];
  44. {
  45.    char  buffer[100],
  46.        rep[80];
  47.    int bytes_read,
  48.        closed,
  49.        file_descriptor,
  50.        i;
  51.    float a;
  52.    long  line_length, offset;
  53.  
  54.    unsigned long position;
  55.  
  56.    struct tiff_header_struct image_header;
  57.  
  58.    read_tiff_header(image_file_name, &image_header);
  59.  
  60.       /****************************************************
  61.       *
  62.       *   Procedure:
  63.       *   Seek to the strip offset where the data begins.
  64.       *   Seek to the first line you want.
  65.       *   Loop over the lines you want to read:
  66.       *      Seek to the first element of the line.
  67.       *      Read the line.
  68.       *      Seek to the end of the data in that line.
  69.       *
  70.       ****************************************************/
  71.  
  72.    file_descriptor = my_open(image_file_name);
  73.    position     = lseek(file_descriptor, image_header.strip_offset, 0);
  74.    position     = seek_to_first_line(file_descriptor, &image_header, il);
  75.  
  76.    for(i=0; i<(ll-il); i++){
  77.       offset     = (ie-1)/(8/image_header.bits_per_pixel);
  78.       position     = lseek(file_descriptor, offset, 1);
  79.       bytes_read = read_line(file_descriptor, array, i, &image_header, ie, le);
  80.       position     = seek_to_end_of_line(file_descriptor, le, &image_header);
  81.       position     = lseek(file_descriptor, 1, 1); /*???*/
  82.    }  /* ends loop over i  */
  83.  
  84.    closed = close(file_descriptor);
  85.  
  86. }  /*  ends read_tiff_image */
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.        /*******************************************************
  94.        *
  95.        *   read_line(...
  96.        *
  97.        *   This function reads bytes from the TIFF file into
  98.        *   a buffer, extracts the numbers from that buffer,
  99.        *   and puts them into a 100x100 array of shorts.
  100.        *   The process depends on the number of bits per
  101.        *   pixel used in the file (4 or 8).
  102.        *
  103.        *******************************************************/
  104.  
  105. read_line(file_descriptor, array, line_number, image_header, ie, le)
  106.    int  file_descriptor, ie, le, line_number;
  107.    short  array[ROWS][COLS];
  108.    struct tiff_header_struct *image_header;
  109. {
  110.    char  buffer[100], first, second;
  111.    float a, b;
  112.    int bytes_read, i;
  113.    unsigned int bytes_to_read;
  114.    union short_char_union scu;
  115.  
  116.    for(i=0; i<100; i++)
  117.       buffer[i] = '\0';
  118.  
  119.         /**********************************************
  120.         *
  121.         *   Use the number of bits per pixel to calculate
  122.         *   how many bytes to read.
  123.         *
  124.         *************************************************/
  125.  
  126.    bytes_to_read = (le-ie)/(8/image_header->bits_per_pixel);
  127.    bytes_read       = read(file_descriptor, buffer, bytes_to_read);
  128.  
  129.    for(i=0; i<bytes_read; i++){
  130.  
  131.         /**********************************************
  132.         *
  133.         *   Use unions defined in cips.h to stuff bytes  
  134.         *   into shorts.                   
  135.         *
  136.         *************************************************/
  137.  
  138.       if(image_header->bits_per_pixel == 8){
  139.        scu.s_num          = 0;
  140.        scu.s_alpha[0]        = buffer[i];
  141.        array[line_number][i] = scu.s_num;
  142.       }  /* ends if bits_per_pixel == 8 */
  143.  
  144.       if(image_header->bits_per_pixel == 4){
  145.  
  146.        scu.s_num             = 0;
  147.        second                       = buffer[i] & 0X000F;
  148.        scu.s_alpha[0]        = second;
  149.        array[line_number][i*2+1] = scu.s_num;
  150.  
  151.        scu.s_num             = 0;
  152.        first                 = buffer[i] >> 4;
  153.        first                 = first & 0x000F;
  154.        scu.s_alpha[0]        = first;
  155.        array[line_number][i*2] = scu.s_num;
  156.  
  157.       }  /* ends if bits_per_pixel == 4 */
  158.  
  159.    }  /*  ends loop over i  */
  160.  
  161.    return(bytes_read);
  162.  
  163. }  /* ends read_line  */
  164.  
  165.  
  166.        /*******************************************************
  167.        *
  168.        *   seek_to_first_line(...
  169.        *
  170.        *******************************************************/
  171.  
  172. seek_to_first_line(file_descriptor, image_header, il)
  173.    int  file_descriptor, il;
  174.    struct tiff_header_struct *image_header;
  175. {
  176.    long offset;
  177.    unsigned long position;
  178.  
  179.    offset   = (il-1)*image_header->image_width/
  180.              (8/image_header->bits_per_pixel);
  181.       /* seek from current position */
  182.    position = lseek(file_descriptor, offset, 1);
  183.    return(position);
  184. }  /* ends seek_to_first_line */
  185.  
  186.  
  187.  
  188.  
  189.        /*******************************************************
  190.        *
  191.        *   seek_to_end_of_line(...
  192.        *
  193.        *******************************************************/
  194.  
  195. seek_to_end_of_line(file_descriptor, le, image_header)
  196.    int file_descriptor, le;
  197.    struct tiff_header_struct *image_header;
  198. {
  199.    int origin;
  200.    long  offset;
  201.    unsigned long position;
  202.  
  203.    offset   = (image_header->image_width-le)/
  204.              (8/image_header->bits_per_pixel);
  205.    origin   = 1;     /* seek from the current position     */
  206.    position = lseek(file_descriptor, offset, origin);
  207.    return(position);
  208. }  /* ends seek_to_end_of_line         */
  209.  
  210.  
  211.  
  212. 
  213.